package com.example.graduatetest.view.model;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.R;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Score;
import com.example.graduatetest.callback.ScoreCallBack;
import com.example.graduatetest.callback.TestNameList;
import com.example.graduatetest.databinding.ActivityScorePersonBinding;
import com.example.graduatetest.model.ScoreModel;
import com.example.graduatetest.utils.Constant;

import java.util.List;

public class ScorePersonViewModel extends ViewModel {
    private Activity activity;
    private ActivityScorePersonBinding binding;
    private Handler handler = new Handler();
    private MutableLiveData<String> class_number;
    private MutableLiveData<String> student_number;
    private String test_name = "暂无考试";
    private MutableLiveData<String> score;
    private String[] strArray;
    private Classes ownClass;
    public void init(Activity activity, ActivityScorePersonBinding binding, Classes ownClass){
        this.activity = activity;
        this.binding = binding;
        this.ownClass = ownClass;
        //获取课程号
        if(class_number == null) class_number = new MutableLiveData<>();
        class_number.setValue(ownClass.getId());
        binding.edClassNumber.setFocusableInTouchMode(false);

        if(student_number == null) student_number = new MutableLiveData<>();
        if(score == null) score = new MutableLiveData<>();
        toolbarInit();
        initStudentNumber();
        initView();
    }

    //初始课程
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar12;
        toolbar.setTitle("个人成绩查询");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });
    }

    private void initStudentNumber(){
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
        if(preferences.getBoolean("isStudent", true)){
            student_number.setValue(preferences.getString("number", "默认"));
        }
        binding.edStudentNumber.setFocusableInTouchMode(false);
    }

    //网络请求
    private void initView(){

        ScoreModel.selectTestName(ownClass.getId(), new TestNameList() {
            @Override
            public void onSuccess(List<String> strList) {
                if(strList != null){
                    strArray = new String[strList.size()];
                    for (int i = 0; i <strList.size() ; i++) {
                        strArray[i] = strList.get(i);
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            updateUI();
                        }
                    });
                }else{
                    strArray = new String[]{"暂无考试"};
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            updateUI();
                        }
                    });
                    Log.e("haha", "暂无考试");
                }

            }

            @Override
            public void onFailed() {

                strArray = new String[]{"网络错误"};
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        updateUI();
                    }
                });
                Log.e("haha", "网络错误");
            }
        });
    }

    //更新spinner
    private void updateUI(){
        ArrayAdapter<String> adapter=new ArrayAdapter<>(activity,android.R.layout.simple_spinner_item, strArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        binding.spinner.setAdapter(adapter);
        binding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                TextView tv = (TextView)view;
                // 修改样式属性
                tv.setTextSize(20f);
                tv.setTextColor(Color.parseColor("#FDCB40"));
                tv.setGravity(Gravity.START);
                test_name = tv.getText().toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO
            }
        });

    }

    public void watchScore(){
        if(class_number.getValue() != null && student_number.getValue() != null && !test_name.equals("暂无考试")){
            ScoreModel.selectOneScore(student_number.getValue(), class_number.getValue(), test_name, new ScoreCallBack() {
                @Override
                public void onSuccess(final Score scoreBean) {
                    if (scoreBean != null){
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                               score.setValue(String.valueOf(scoreBean.getScore()));
                               binding.textGrade.setText(Constant.setGradeFun(scoreBean.getScore(), scoreBean.getScore_format()));
                            }
                        });
                    }
                }

                @Override
                public void onFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(activity, "查询成绩失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }else{
            Toast.makeText(activity, "信息不全无法查询", Toast.LENGTH_SHORT).show();
        }

    }

    public MutableLiveData<String> getScore() {
        return score;
    }


    public MutableLiveData<String> getStudent_number() {
        return student_number;
    }

    public MutableLiveData<String> getClass_number() {
        return class_number;
    }
}
